package ruan_kao;

enum TYPE {
    COMMA, DOTS, LINE
}

interface PrintStrategy {
    public void doPrint(Interval val); //1 或 abstract void doPrint(Interval val)
}

class Interval {
    private double lower;
    private double upper;

    public Interval(double lower, double upper) {
        this.lower = lower;
        this.upper = upper;
    }

    public double getLower() {
        return lower;
    }

    public double getUpper() {
        return upper;
    }

    public void printIntervals(PrintStrategy ptr) {
        ptr.doPrint(this); //2
    }
}

class PrintIntervalsComma implements PrintStrategy {
    public void doPrint(Interval val) {
        System.out.println("[" + val.getLower() + "," + val.getUpper() + "]");
    }
}

class PrintIntervalsDots implements PrintStrategy {
    public void doPrint(Interval val) {
        System.out.println("[" + val.getLower() + "..." + val.getUpper() + "]");
    }
}

class PrintIntervalsLine implements PrintStrategy {
    public void doPrint(Interval val) {
        System.out.println("[" + val.getLower() + "-" + val.getUpper() + "]");
    }
}

public class y2023_m05 {
    public static PrintStrategy getStrategy(TYPE type) {
        PrintStrategy st = null;
        switch (type) {
            case COMMA:
                st = new PrintIntervalsComma(); //3
                break;
            case DOTS:
                st = new PrintIntervalsDots(); //4
                break;
            case LINE:
                st = new PrintIntervalsLine(); //5
                break;
        }

        return st;
    }

    public static void main(String[] args) {
        Interval a = new Interval(1.7, 2.1);
        a.printIntervals(getStrategy(TYPE.COMMA));
        a.printIntervals(getStrategy(TYPE.DOTS));
        a.printIntervals(getStrategy(TYPE.LINE));
    }
}
